<?php
class SiteManager_NewsController extends SiteManagerController
{

	public function indexAction() {
		$iPage = intval($this->getRequest()->getParam('page', 1));
    	$this->view->news = Obj_News::getAllNews($iPage);
    }

   	public function editAction() {
   		$id = (int) $this->getRequest()->getParam('id', 0);
		$addnew = intval($this->getRequest()->getParam('addnew', 0)) === 1 ? true : false;
		$arNews = Obj_News::getOneNews($id);
		if ($addnew || $arNews === false) {
    		$this->view->news = array(
	    		'ID' 			=> 0,
	    		'Title' 		=> '',
	    		'Content' 		=> '',
	    		'active' 		=> 0,
	    		'author' 		=> '',
	    		'seoTitle' 		=> '',
	    		'seoKeywords' 	=> '',
	    		'seoDescription'=> '',
	    		'date' 			=> ''
    		);
    	}
    	else {
    		$this->view->news = $arNews;
    	}
    }

    public function saveAction() {
		if (!$this->_isAJAX) {
				throw new Zend_Exception(_('Эта функция Вам недоступна'));
		}
		$id = (int) $this->getRequest()->getParam('ID', 0);
		$arNews = array();
		$arNews['Title'] = trim($this->getRequest()->getParam('Title', ''));
		$arNews['Content'] = trim($this->getRequest()->getParam('Content', ''));
		$arNews['active'] = (int)intval($this->getRequest()->getParam('active', 0));
		$arNews['seoTitle'] = trim($this->getRequest()->getParam('seoTitle', ''));
		$arNews['seoKeywords'] = trim($this->getRequest()->getParam('seoKeywords', ''));
		$arNews['seoDescription'] = trim($this->getRequest()->getParam('seoDescription', ''));
		$arNews['date'] = date("Y-m-d H:i:s", time());
		$arNews['alias'] = Obj_News::getAlias($arNews['Title'], $id);
        $arNews['author'] = 0;
    	if (Zend_Auth::getInstance()->hasIdentity()) {
	    	$user = Zend_Auth::getInstance()->getIdentity();
    		$arNews['author'] = $user->ID;
    	}
        $allNews = Obj_News::getNewsArrayList();
        $errors = array('is_error' => 0, 'err_string' => '');
        if($id != 0){
			$current_news = Obj_News::getOneNews($id);
			
			if(	
				$arNews['Title'] == $current_news['Title'] &&
				$arNews['Content'] == $current_news['Content'] && 
				$arNews['active'] == $current_news['active'] && 
				$arNews['seoTitle'] == $current_news['seoTitle'] && 
				$arNews['seoDescription'] == $current_news['seoDescription'] && 
				$arNews['seoKeywords'] == $current_news['seoKeywords']
			){
				$errors['is_error'] = 1;
				$errors['err_string'] .= "Даная новость уже существует. Нет необходимости в повторном добавлении!\n";
			}

			if($arNews['Title'] == ''){
				$errors['is_error'] = 1;
				$errors['err_string'] .= "Задайте заголовок страницы!\n";
			}
			if(strlen($arNews['Title']) > 254){
				$errors['is_error'] = 1;
				$errors['err_string'] .= "Заголовок страницы слишком длинный!\n";
			}
			if(strip_tags($arNews['Content']) == ""){
				$errors['is_error'] = 1;
				$errors['err_string'] .= "Контент страницы не может быть пустым!\n";
			}
		} else {
			if($arNews['Title'] == ''){
				$errors['is_error'] = 1;
				$errors['err_string'] .= "Задайте заголовок страницы!\n";
			}
			if(strlen($arNews['Title']) > 254){
				$errors['is_error'] = 1;
				$errors['err_string'] .= "Заголовок страницы слишком длинный!\n";
			}
			if(trim(strip_tags($arNews['Content'])) == ""){
				$errors['is_error'] = 1;
				$errors['err_string'] .= "Контент страницы не может быть пустым!\n";
			}
		}
		foreach ($allNews as $v){
			if ($v['Title'] == $arNews['Title'] && $id != 0 && $id != $v['ID']){
				$errors['is_error'] = 1;
				$errors['err_string'] .= "Новость з таким заголовком уже существует!\n";					
			}
			
		}
		if($errors['is_error'] == 1){
			$this->view->error = $errors['err_string'];
			$this->_helper->json(array('error' => 1, 'errorStr' => $errors['err_string']), true, false);
			return;
			die();
		}
		$npid = (int)Obj_News::saveNews($arNews, $id);		
		if ($arNews['active'] === 1){
    		Obj_System_SearchIndex::write($npid, CONTENT_TYPE_NEWS, $arNews['Title'], strip_tags($arNews['Content']));
    	} else {
    		Obj_System_SearchIndex::delete($npid, CONTENT_TYPE_NEWS);
    	} 
		$this->_helper->json(array('error' => 0, 'redirect' => $this->view->url(array('module' => 'site-manager', 'controller'=>'news', 'action' => 'index'))), true, false);
    }

    public function deleteAction() {
        if (!$this->_isAJAX) {
    		throw new Zend_Exception(_('Эта функция Вам недоступна'));
    	}
    	$id = (int) $this->getRequest()->getParam('id', 0);
    	Obj_News::delOneNews($id);
    	Obj_System_SearchIndex::delete($id, CONTENT_TYPE_NEWS);
    	$this->_helper->json(array('error' => 0, 'redirect' => $this->view->url(array('module' => 'site-manager', 'controller'=>'news', 'action' => 'index'))), true, false);
    }

    public function groupedDeleteAction(){
    	if (!$this->_isAJAX) {
    		throw new Zend_Exception(_('Эта функция Вам недоступна'));
    	}
    	$arDelete = array();
    	$arDelete = $this->getRequest()->getParam('arDelete', null);
    	foreach ($arDelete as $k => $v){
	    	$id = (int) $v;
	    	Obj_News::delOneNews($id);
	    	Obj_System_SearchIndex::delete($id, CONTENT_TYPE_NEWS);
    	}
		$this->_helper->json(array('error' => 0, 'redirect' => $this->view->url(array('module' => 'site-manager', 'controller'=>'news', 'action' => 'index'))), true, false);
    }

    public function stateChangerAction() {
		if (!$this->_isAJAX) {
    		throw new Zend_Exception(_('Эта функция Вам недоступна'));
    	}
    	$id = (int) $this->getRequest()->getParam('id', 0);
    	$arNews = Obj_News::getOneNews($id);
    	if(intval($arNews['active']) === 1){
    		$arNews['active'] = 0;
    		$nid = Obj_News::saveNews($arNews, $id);
    		Obj_System_SearchIndex::delete($nid, CONTENT_TYPE_NEWS);
    	} else {
    		$arNews['active'] = 1;
    		$nid = Obj_News::saveNews($arNews, $id);
    		Obj_System_SearchIndex::write($nid, CONTENT_TYPE_NEWS, $arNews['Title'], $arNews['Content']);
    	}
    	$this->_helper->json(array('error' => 0, 'redirect' => $this->view->url(array('module' => 'site-manager', 'controller'=>'news', 'action' => 'index'))), true, false);
    }
}